<template>
    <div>
        <div class="zhsf-xcbd-b-ct-left" style="margin: 12% auto 0;width: 708px;">
            <div :class="
                sfzState == 0 ? 'zhsf-xcbd-b-ct-l-con' : 
                sfzState == 1 ? 'zhsf-xcbd-b-ct-l-con state-box01' : 
                sfzState == 2 ? 'zhsf-xcbd-b-ct-l-con state-box02' : 'zhsf-xcbd-b-ct-l-con state-box03'
            " @click="clickToggle()"><!-- 添加state-box01扫描中..state-box02读取成功..state-box03读取失败  -->
                <a href="javascript:;" class="zhsf-xcbd-b-ct-l-c-btn" @click="initReadCard()">再次读取</a>
            </div>
        </div>
        <div class="sysm-brbox-bom text-ac pore-box">
            <!-- <p class="sysm-brbbom-false" v-show="sfzState == 2">{{errorTxt}}</p> -->
            <a href="javascript:;" class="sysm-brbbom-btn" v-show="sfzState == 2" @click="toRouter('yzsf')">验证身份</a>
            <a href="javascript:;" class="sysm-brbbom-btn true-obj ml50" v-show="sfzState == 2" @click="toRouter('sgdj')">手工登记</a>
        </div>
      </div>
</template>

<script>
import {idcardreader} from '@/views/modules/webModu/terminal/js/kydevmonit'
export default {
    data(){
        return {
            sfzState : 0,
            errorTxt : '识别失败',
            dataForm:{
                loginName:'',
                identityType:'1',
                identityTypeName:'身份证',
                identityNo:'',
                sex:'1',
                userType:'2',
                birthday:'',
                hometown:'',
                avatar:'',
                idcardPhoto:''
            }
        }
    },
    created(){
        this.dataForm.avatar=this.vtp.get("imgPath");
        // this.initReadCard();
        this.initData();
        let _this = this;
        setTimeout(function(){
            _this.toRouter("yzsf")
        },2000)
    },
    methods:{
        toRouter(o){
            this.vtp.toRouter(this,o);
        },
        initData(){
            var imgPath = this.vtp.get("imgPath");
            console.log("imgPath",imgPath);
            this.dataForm={
                loginName:'身份1',
                identityType:'1',
                identityTypeName:'身份证',
                identityNo:'11111111',
                sex:'1',
                userType:'2',
                birthday:'2020-03-03',
                hometown:'广东',
                avatar:imgPath,
                idcardPhoto:imgPath
            }
            this.vtp.set("userInfo",this.dataForm)
        },
        clickToggle(){
            if(this.sfzState == 0){
                this.sfzState = 1;
            }else if(this.sfzState == 1){
                this.sfzState = 2;
            }else if(this.sfzState == 2){
                this.sfzState = 3;
            }else{
                this.sfzState = 0;
            }
        },
    initReadCard(){
        this.sfzState=0;
        this.sfzPopupState=true;
        let _this=this;
        idcardreader.connected(function(){
            idcardreader.getState(function(state,exception_code){
                console.log("getState返回:"+state+ " " + exception_code);
                if(exception_code=="0"){
                    _this.getidcard();
                }else{
                    _this.sfzState=3;
                }
            });
        });
    },
    getidcard(){
        let _this=this;
        idcardreader.startScan({},function(return_code){
            _this.sfzState=1;
            console.log("startScan返回码: " + return_code);
        });
        idcardreader.startScanCompleted(function(return_code,info){
            console.log("startScanCompleted返回: " + return_code + " " + info);
            if(return_code=="0"){
                _this.sfzState=2;
                var infos = info.split("|");
                var aaa = "身份证：" +infos[0]+"\n姓名："+infos[1]+"\n性别："+infos[2]+"\n名族："+infos[3]+"\n生日："+infos[4]
                    +"\n户籍地："+infos[5]+"\n签发机关："+infos[6]+"\n有效期："+infos[7]+"\n照片："+infos[8];
                //"412721xxxxxxxxxxxx | 无名 | 男 | 汉 | 20180720 |  XX省XX县XX乡XX村 | XX县公安局 | 20180720-20280720 | /tmp/412721xxxxxxxxxxxx.jpg"
                for(var i=0;i<infos.length;i++){
                    infos[i]=trim(info_list[i]);
                }
                _this.dataForm.loginName=infos[1];
                _this.dataForm.identityNo=infos[0];
                _this.dataForm.sex=infos[2];
                _this.dataForm.birthday=infos[4];
                _this.dataForm.hometown=infos[5];
                _this.dataForm.identityType="1";
                _this.dataForm.identityTypeName="身份证";
                this.$http({
                    url: this.$http.adornUrl(
                    "/zfuser/zfinvolvedregistration/saveBase64Img"
                    ),
                    method: "post",
                    data: this.$http.adornData({
                    base64Str: infos[8]
                    })
                }).then(({ data }) => {
                    _this.dataForm.idcardPhoto=data;
                });
                _this.vtp.set("userInfo", _this.dataForm);
                _this.toRouter("yzsf")
            }else if(return_code=="3000107"){
                //超时
                _this.sfzState=3;
            }else {
                _this.sfzState=3;
            }
        })
    }
    }
}
</script>

<style>

</style>